package com.yang.frost.blueberry.weixin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yang.frost.blueberry.util.HttpRequestUtil;
import com.yang.frost.blueberry.weixin.config.WxConfig;
import com.yang.frost.blueberry.weixin.params.WxTagsParams;
import com.yang.frost.blueberry.weixin.service.WxService;
import com.yang.frost.blueberry.weixin.service.WxUmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 10296
 * @date 2021/4/14
 * @Description:
 */
@Service
public class WxUmsServiceImpl implements WxUmsService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxService wxService;
    @Autowired
    private HttpRequestUtil requestUtil;

    @Override
    public JSONObject createTags(WxTagsParams paramsMap) throws Exception {
        List<Map<String, Object>> retList = new ArrayList<>();
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.CREATE_TAGS, accessToken);
        JSONObject parent = new JSONObject();
        JSONObject childObj = new JSONObject();
        childObj.put("name", paramsMap.getName());
        parent.put("tag", childObj);
        JSONObject result = requestUtil.httpsRequest(url, "POST", parent.toString());
        logger.info(result + "");
        return result;

    }

    @Override
    public Map<String, Object> getTags() throws Exception {
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.GET_TAGS, accessToken);
        JSONObject result = requestUtil.httpsRequest(url, "GET", null);
        logger.info(result + "");
        return result;
    }

    @Override
    public Map<String, Object> updateTags(WxTagsParams params) throws Exception {
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.UPDATE_TAGS, accessToken);
        JSONObject result = requestUtil.httpsRequest(url, "POST", params.getContent());
        logger.info(result + "");
        return result;
    }

    @Override
    public Map<String, Object> deleteTags(WxTagsParams params) throws Exception {
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.DELETE_TAGS, accessToken);
        JSONObject parent = new JSONObject();
        JSONObject childObj = new JSONObject();
        childObj.put("id", params.getTagId());
        parent.put("tag", childObj);
        JSONObject result = requestUtil.httpsRequest(url, "POST", parent.toString());
        logger.info(result + "");
        return result;
    }

    @Override
    public Map<String, Object> getUserByTag(WxTagsParams params) throws Exception {
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.GET_USER_TAGS, accessToken);
        JSONObject result = requestUtil.httpsRequest(url, "POST", params.getContent());
        logger.info(result + "");
        return result;
    }

    @Override
    public Map<String, Object> batchTaggingMembersTags(WxTagsParams params) throws Exception {
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.BATCHTAGGING_MEMBERS_TAGS, accessToken);
        JSONObject result = requestUtil.httpsRequest(url, "POST", params.getContent());
        logger.info(result + "");
        return result;
    }

    @Override
    public Map<String, Object> batchUnTaggingMembersTags(WxTagsParams params) throws Exception {
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.BATCHUNTAGGING_MEMBERS_TAGS, accessToken);
        JSONObject result = requestUtil.httpsRequest(url, "POST", params.getContent());
        logger.info(result + "");
        return result;
    }

    @Override
    public Map<String, Object> getIdListTags(WxTagsParams params) throws Exception {
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.GETIDLIST_TAGS, accessToken);
        JSONObject paramsMap = new JSONObject();
        paramsMap.put("openid", params.getOpenid());
        JSONObject result = requestUtil.httpsRequest(url, "POST", paramsMap.toString());
        logger.info(result + "");
        return result;
    }

    @Override
    public Map<String, Object> updateRemarkUserInfo(WxTagsParams params) throws Exception {
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.UPDATEREMARK_USER_INFO, accessToken);
        JSONObject result = requestUtil.httpsRequest(url, "POST", params.getContent());
        logger.info(result + "");
        return result;
    }

    @Override
    public Map<String, Object> getUserInfo(WxTagsParams params) throws Exception {
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.USER_INFO, accessToken, params.getOpenid());
        JSONObject result = requestUtil.httpsRequest(url, "GET", null);
        logger.info(result + "");
        return result;
    }

    @Override
    public Map<String, Object> batchGetUserInfo(WxTagsParams params) throws Exception {
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.BATCHGET_INFO, params.getOpenid());
        JSONObject result = requestUtil.httpsRequest(url, "GET", null);
        logger.info(result + "");
        return result;
    }

    @Override
    public Map<String, Object> getUserList(WxTagsParams params) throws Exception {
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.GET_USER_LIST, accessToken, params.getOpenid());
        JSONObject result = requestUtil.httpsRequest(url, "GET", null);
        logger.info(result + "");
        return result;
    }

    @Override
    public Map<String, Object> getBlackList(WxTagsParams params) throws Exception {
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.GETBLACKLIST_MEMBERS, accessToken);
        JSONObject result = requestUtil.httpsRequest(url, "POST", params.getContent());
        logger.info(result + "");
        return result;
    }

    @Override
    public Map<String, Object> batchBlacklist(WxTagsParams params) throws Exception {
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.BATCHBLACKLIST_MEMBERS, accessToken);
        JSONObject result = requestUtil.httpsRequest(url, "POST", params.getContent());
        logger.info(result + "");
        return result;
    }

    @Override
    public Map<String, Object> batchUnBlacklist(WxTagsParams params) throws Exception {
        String accessToken = wxService.getAccessToken();
        String url = String.format(WxConfig.BATCHUNBLACKLIST_MEMBERS, accessToken);
        JSONObject result = requestUtil.httpsRequest(url, "POST", params.getContent());
        logger.info(result + "");
        return result;
    }


}
